package com.example.forum.controller;

import ch.qos.logback.classic.Logger;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.service.impl.ArticleServiceImpl;
import com.example.forum.service.impl.BoardServiceImpl;
import com.mysql.jdbc.log.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api("版块接口")
@RestController
@RequestMapping("/board")
public class BoardController {


    @Autowired
    private BoardServiceImpl boardService;


    @Autowired
    private ArticleServiceImpl articleService;
    @GetMapping("/topList")
    @ApiOperation("获取版块列表")
    public AppResult topList(@ApiParam("获取前几个") @RequestParam(value = "num",required = false)Integer num) {
        List<Board> list = boardService.selectByNum(7);
        if(list.isEmpty() || list.size() < 0) {
            log.error(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return AppResult.fail(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        return AppResult.success(list);

    }

    @ApiOperation("根据boardId查找列表文章")
    @RequestMapping("/getById")
    public AppResult getById(@RequestParam(value = "id") @ApiParam("论坛板块id") @NonNull Long boardId) {
        Board board = boardService.getBoardById(boardId);
        if(board.getId() != boardId) {
            log.error(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return AppResult.fail(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        log.info("根据boardId查找成功");
        return AppResult.success(board);
    }

}
